package com.lanyou.cook.camel.util;

import java.util.HashMap;
import java.util.Map;

import org.apache.camel.component.file.remote.FtpConfiguration;
import org.apache.camel.component.file.remote.FtpEndpoint;
import org.apache.camel.component.file.remote.FtpOperations;
import org.apache.camel.component.file.remote.RemoteFileComponent;
import org.apache.camel.component.file.remote.RemoteFileOperations;
import org.apache.camel.spi.UriParam;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPClientConfig;
import org.apache.commons.net.ftp.FTPFile;

/**
 * 继承FtpEndpoint，大部分方式属性使用FtpEndpoint的即可，可根据自己的需求做相应改造
 * 
 * @author ly-lihailang
 *
 * @param <T>FTPFile
 */
public class FtpEndpointChild<T extends FTPFile> extends FtpEndpoint<FTPFile> {
	
	/**
	 * 保存ftpFileFilter的属性值
	 */
	@UriParam(label = "advanced", prefix = "ftpFileFilter.", multiValue = true)
    protected Map<String, Object> ftpFileFilterParameters;
	/**
	 * 提供通过uri创建实例的方法
	 */
	@UriParam(label = "advanced")
    protected FtpFileFilter<FTPFile> ftpFileFilter;
	
	public FtpEndpointChild() {
	}

	public FtpEndpointChild(String uri, RemoteFileComponent<FTPFile> component,
			FtpConfiguration configuration) {
		super(uri, component, configuration);
		this.configuration = configuration;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.apache.camel.component.file.remote.FtpEndpoint#createRemoteFileOperations
	 * ()
	 */
	public RemoteFileOperations<FTPFile> createRemoteFileOperations()
			throws Exception {
		FTPClient client = this.ftpClient;

		if (client == null) {
			client = createFtpClient();
		}

		if (getConfiguration().getReceiveBufferSize() > 0) {
			client.setBufferSize(getConfiguration().getReceiveBufferSize());
		}

		if (getConfiguration().getConnectTimeout() > -1) {
			client.setConnectTimeout(getConfiguration().getConnectTimeout());
		}
		if (getConfiguration().getSoTimeout() > -1) {
			this.soTimeout = getConfiguration().getSoTimeout();
		}
		this.dataTimeout = getConfiguration().getTimeout();

		if (this.ftpClientParameters != null) {
			Map<String, Object> localParameters = new HashMap<String, Object>(
					this.ftpClientParameters);

			Object timeout = localParameters.remove("soTimeout");
			if (timeout != null) {
				this.soTimeout = ((Integer) getCamelContext()
						.getTypeConverter().convertTo(Integer.TYPE, timeout))
						.intValue();
			}

			timeout = localParameters.remove("dataTimeout");
			if (timeout != null) {
				this.dataTimeout = ((Integer) getCamelContext()
						.getTypeConverter().convertTo(Integer.TYPE,
								Integer.valueOf(this.dataTimeout))).intValue();
			}
			setProperties(client, localParameters);
		}

		if (this.ftpClientConfigParameters != null) {
			if (this.ftpClientConfig == null) {
				this.ftpClientConfig = new FTPClientConfig();
			}
			Map<String, Object> localConfigParameters = new HashMap<String, Object>(
					this.ftpClientConfigParameters);
			setProperties(this.ftpClientConfig, localConfigParameters);
		}

		if (this.dataTimeout > 0) {
			client.setDataTimeout(this.dataTimeout);
		}

		if (this.log.isDebugEnabled()) {
			this.log.debug(
					"Created FTPClient [connectTimeout: {}, soTimeout: {}, dataTimeout: {}, bufferSize: {}, receiveDataSocketBufferSize: {}, sendDataSocketBufferSize: {}]: {}",
					new Object[] {
							Integer.valueOf(client.getConnectTimeout()),
							Integer.valueOf(getSoTimeout()),
							Integer.valueOf(this.dataTimeout),
							Integer.valueOf(client.getBufferSize()),
							Integer.valueOf(client
									.getReceiveDataSocketBufferSize()),
							Integer.valueOf(client
									.getSendDataSocketBufferSize()), client });
		}
		
		client.setDefaultTimeout(30 * 1000);
		client.setConnectTimeout(30 * 1000);
		client.setDataTimeout(30 * 1000);
		client.enterLocalPassiveMode();
		client.setRemoteVerificationEnabled(false);
		//如果ftpFileFilterParameters不为空且外界并没创建ftpFileFilter，则在此创建并给该对象的属性赋值
		if(ftpFileFilterParameters!=null&&ftpFileFilter==null){
			ftpFileFilter = createFtpFileFilter("");
			setProperties(ftpFileFilter, ftpFileFilterParameters);
			this.setFilter(ftpFileFilter);
		}
		FtpOperations operations = new FtpOperations(client,
				getFtpClientConfig());
		operations.setEndpoint(this);
		return operations;
	}
	
	/**
	 * 创建FtpFileFilter实例
	 * @param regex 正则表达式
	 * @return 创建好的FtpFileFilter实例
	 */
	protected FtpFileFilter<FTPFile> createFtpFileFilter(String regex){
		FtpFileFilter<FTPFile> ftpFileFilter = null;
		//可按正则表达式创建，如果没有则按空构造器创建空实例
		if(regex!=null&&!regex.equals("")){
			ftpFileFilter = new FtpFileFilter<FTPFile>(regex);
		}else{
			ftpFileFilter = new FtpFileFilter<FTPFile>();
		}
		return ftpFileFilter;
	}
	
	public Map<String, Object> getFtpFileFilterParameters() {
		return ftpFileFilterParameters;
	}

	public void setFtpFileFilterParameters(
			Map<String, Object> ftpFileFilterParameters) {
		this.ftpFileFilterParameters = ftpFileFilterParameters;
	}

	public FtpFileFilter<FTPFile> getFtpFileFilter() {
		return ftpFileFilter;
	}

	public void setFtpFileFilter(FtpFileFilter<FTPFile> ftpFileFilter) {
		this.ftpFileFilter = ftpFileFilter;
	}
	
}
